package com.senyint.imsdk.packet;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

public class SimpleEventIQ extends IQ {

    /**
	 * 
	 */
	private static final long serialVersionUID = -1111075639103530292L;
	public static final String TAG = SimpleEventIQ.class.getSimpleName();
	
	/* the data packet extension */
    private final SimpleEventExtension data;
    
    /**
     * Constructor.
     * 
     * @param data data packet extension containing the encoded data
     */
    public SimpleEventIQ(SimpleEventExtension data) {
        if (data == null) {
            throw new IllegalArgumentException("Data must not be null");
        }
        this.data = data;

        /*
         * also set as packet extension so that data packet extension can be
         * retrieved from IQ stanza and message stanza in the same way
         */
        addExtension(data);
    }

    public SimpleEventExtension getPacketExtension() {
        return this.data;
    }

    public String getChildElementXML() {
    	if(data != null)
    		return this.data.toXML();
    	
    	return null ;
    }

    public static class Provider implements IQProvider {
		@Override
		public IQ parseIQ(XmlPullParser parser) throws Exception {
			SimpleEventIQ info = null ;
		    String type = null ;
		    String event = null ;
	        boolean done = false;
	        while (!done) {
	            int eventType = parser.next();
	            if (eventType == XmlPullParser.START_TAG) {
	                if (parser.getName().equals("type"))
	                    type = parser.nextText()  ;//parser.getAttributeValue("", "node"));
	                else  if (parser.getName().equals("event"))
	                	event = parser.nextText();
	            } else if (eventType == XmlPullParser.END_TAG) {
	                if (parser.getName().equals(SimpleEventExtension.ELEMENT_NAME)) {
	                    done = true;
	                }
	            }
	        }
	        if(type != null && event != null ){
	        	info = new SimpleEventIQ(new SimpleEventExtension(type,event));
	        }
	        
			return info ;
		}
    	
    }
    
}
